package com.gaoding.shared.dict.controller;

import com.gaoding.framework.web.dto.PageInfo;
import com.gaoding.framework.web.util.WebUtils;
import com.gaoding.grpc.client.annotation.GdGrpcClient;
import com.gaoding.grpc.common.protobuf.Common;
import com.gaoding.grpc.common.utils.GdBeanUtils;
import com.gaoding.shared.dict.dto.request.CreateDictTextContentDTO;
import com.gaoding.shared.dict.dto.request.QueryDictTextContentDTO;
import com.gaoding.shared.dict.dto.request.UpdateDictTextContentDTO;
import com.gaoding.shared.dict.dto.response.DictTextContentRespDTO;
import com.gaoding.shared.dict.rpc.service.CreateDictTextContentRequest;
import com.gaoding.shared.dict.rpc.service.DictTextContentServiceGrpc;
import com.gaoding.shared.dict.rpc.service.GetTextContentListRequest;
import com.gaoding.shared.dict.rpc.service.UpdateDictTextContentRequest;
import com.google.protobuf.Int32Value;
import com.google.protobuf.Int64Value;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mobai
 * @since 2021/4/15 14:38
 */
@RestController
@RequestMapping("/contents")
@Api(tags = "字典内容配置接口")
@CrossOrigin
public class DictTextContentController extends BaseController<DictTextContentRespDTO> {

    @GdGrpcClient
    private DictTextContentServiceGrpc.DictTextContentServiceBlockingStub client;


    @GetMapping("list")
    @ApiOperation("分页查询")
    public List<DictTextContentRespDTO> list(QueryDictTextContentDTO query) {
        int pageNum = Integer.max(query.getPageNum() - 1, 0);
        Common.Page page = Common.Page.newBuilder().setPageSize(query.getPageSize()).setPageNum(pageNum).buildPartial();
        val queryParam = GetTextContentListRequest.newBuilder().setServiceCode(query.getServiceCode() != null ? query.getServiceCode() : "").setLanguageCode(query.getLanguageCode() != null ? query.getLanguageCode() : "").setTextKey(query.getTextKey() != null ? query.getTextKey() : "").setPage(page);
        if (query.getTextType() != null) {
            queryParam.setTextType(Int32Value.of(query.getTextType()));
        }
        val result = client.getList(queryParam.build())
                .getDataList().stream().map(data -> {
                    DictTextContentRespDTO respDTO = new DictTextContentRespDTO();
                    GdBeanUtils.copyProperties(data, respDTO);
                    return respDTO;
                }).collect(Collectors.toList());
        WebUtils.buildPageInfo(PageInfo.builder().num(query.getPageNum()).size(query.getPageSize()).build());
        return result;
    }

    @PostMapping("")
    @ApiOperation("新增单个字典")
    public Long create(@Valid @RequestBody CreateDictTextContentDTO createDictTextContentDTO) {
        CreateDictTextContentRequest.Builder builder = CreateDictTextContentRequest.newBuilder();
        GdBeanUtils.copyProperties(createDictTextContentDTO, builder);
        builder.setTextType(Int32Value.of(createDictTextContentDTO.getTextType()));
        Int64Value int64Value = client.create(builder.build());
        return int64Value.getValue();

    }

    @PutMapping("/{id}")
    @ApiOperation("更新单个字典")
    public void update(@PathVariable("id") Long id, @RequestBody @Valid UpdateDictTextContentDTO updateDictTextContentDTO) {
        updateDictTextContentDTO.setId(id);
        UpdateDictTextContentRequest.Builder builder = UpdateDictTextContentRequest.newBuilder();
        GdBeanUtils.copyProperties(updateDictTextContentDTO, builder);
        builder.setId(Int64Value.of(updateDictTextContentDTO.getId()));
        builder.setSort(Int32Value.of(updateDictTextContentDTO.getSort()));
        builder.setTextType(Int32Value.of(updateDictTextContentDTO.getTextType()));
        client.update(builder.build());

    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除单个字典")
    public void delete(@PathVariable("id") Long id) {
        client.delete(Int64Value.of(id));
    }

    @Override
    protected String[] args(DictTextContentRespDTO dictTextContent) {
        return new String[]{dictTextContent.getServiceCode(),dictTextContent.getLanguageCode(),dictTextContent.getTextKey(),dictTextContent.getServiceCode(),dictTextContent.getLanguageCode(),dictTextContent.getTextType().toString(),dictTextContent.getTextKey(),dictTextContent.getTextContent()};
    }
}
